<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="search-container">
        <el-input v-model="searchQuery.title" placeholder="请输入内容" style="width: 200px;margin-right:10px;" class="filter-item" @keyup.enter.native="handleFilter" />
        <el-date-picker
          v-model="select_month"
          type="month"
          placeholder="选择月或周"
          style="width: 120px;margin-right:1rem;"
          :clearable="true"
          :picker-options="pickerOptions"
          @change="handleSelectMonth"
        />
        <el-select v-model="searchQuery.project_id" class="filter-item" clearable placeholder="请选择项目" style="width: 200px;margin-right:10px;" @change="handleChangeProject">
          <el-option v-for="item in projectOptions" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
        <el-select v-model="searchQuery.type" class="filter-item" placeholder="请选择类型" style="width: 150px;margin-right:10px;" @change="handleChangeType">
          <el-option v-for="item in typeOptions" :key="item" :label="item" :value="item" />
        </el-select>
        <el-select v-model="searchQuery.execute_by" clearable class="filter-item" placeholder="请选择执行人" style="width: 150px;margin-right:10px;" @change="handleChangeUser">
          <el-option v-for="item in usersOptions" :key="item.id" :label="item.nickname" :value="item.id" />
        </el-select>
        <el-button class="filter-item" type="primary" icon="el-icon-search" style="margin-right:1rem;" @click="handleFilter">
          查询
        </el-button>
        <!-- <el-badge value="显示已完成" type="info" class="item">
          <el-switch
            v-model="searchQuery.done"
            active-color="#13ce66"
            style="margin-top:0.5rem;"
             @change="handleChangeRadio"
          />
        </el-badge> -->
        <el-radio-group v-model="searchQuery.done" @change="handleChangeRadio">
          <el-radio-button :label="0">未完成</el-radio-button>
          <el-radio-button :label="1">已完成</el-radio-button>
        </el-radio-group>
      </div>
      <pagination v-show="total>0" :total="total" :page.sync="searchQuery.pageNo" :layout="'total, sizes, prev, pager, next, jumper'" :limit.sync="searchQuery.pageSize" @pagination="getManageDatas" />
    </div>

    <el-table
      :key="tableKey"
      v-loading="pageLoading"
      :data="listData"
      border
      fit
      style="width: 100%;"
    >
      <el-table-column label="名称" align="left">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务系数" width="100px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.weight }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="100px" align="center">
        <template slot-scope="{row}">
          <el-tag effect="light" :type="row.status | golbalStatusFilter">
            {{ row.status | golbalStatusNameFilter }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="优先级" width="100px" align="center">
        <template slot-scope="{row}">
          <el-tag effect="dark" :type="row.priority | golbalPriorityFilter">
            {{ row.priority | golbalPriorityNameFilter }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="归属项目" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.project.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="执行人" width="150px" align="center">
        <template slot-scope="{row}">
          <span v-if="!(row.execute_by && row.execute_by.length)">-</span>
          <span v-for="(user, idx) of row.execute_by" :key="user.id">{{ user.nickname }}{{ idx === row.execute_by.length - 1 ? '' : '、' }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="!searchQuery.done" label="进度" width="100px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.progress }}</span>
        </template>
      </el-table-column>
      <el-table-column label="截止时间" width="100px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.deadline_at | golbalParseTimeFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="searchQuery.done" label="完成时间" width="100px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.complete_at | golbalParseTimeFilter }}</span>
        </template>
      </el-table-column>
    </el-table>

  </div>
</template>

<script>
import { dealWithMonthDay, dealWithWeekDay } from '@/utils/index'
import Pagination from '@/components/Pagination'
import * as ManageApi from '@/api/manage'
import * as UserApi from '@/api/user'
import * as ProjectApi from '@/api/project'

let week = false
let weekText = ''
const dateMonth = new Date()
dateMonth.setMonth(dateMonth.getMonth())
dateMonth.setDate(1)

export default {
  name: 'Summary',
  components: { Pagination },
  data() {
    return {
      tableKey: 0,
      total: 0,
      select_month: dateMonth,
      weekText: '',
      pickerOptions: {
        shortcuts: [{
          text: '本周',
          onClick(picker) {
            week = true
            weekText = this.text
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24 * (date.getDay() - 1))
            picker.$emit('pick', date)
            setTimeout(() => {
              week = false
            }, 0)
          }
        }, {
          text: '上周',
          onClick(picker) {
            week = true
            weekText = this.text
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24 * (date.getDay() + 6))
            picker.$emit('pick', date)
            setTimeout(() => {
              week = false
            }, 0)
          }
        }]
      },
      searchQuery: {
        title: '',
        pageNo: 1,
        pageSize: 10,
        start_time: '',
        end_time: '',
        project_id: '',
        type: '任务',
        done: 1,
        execute_by: ''
      },
      projectOptions: [],
      typeOptions: ['任务', '需求', '缺陷'],
      usersOptions: [],
      pageLoading: false,
      listData: []
    }
  },
  mounted() {
    this.handleSelectMonth(this.select_month)
    this.getUsers()
    this.getProjects()
  },
  methods: {
    async getManageDatas(pageQuery) {
      if (pageQuery) {
        this.searchQuery = Object.assign(this.searchQuery, pageQuery)
      }
      this.listData = []
      this.pageLoading = true
      this.searchQuery['project_id'] = this.searchQuery['project_id'] || null
      this.searchQuery['execute_by'] = this.searchQuery['execute_by'] || null
      const { data } = await ManageApi.getManageSummarys(this.searchQuery)
      if (data && data.total) {
        this.listData = data.items
        this.total = data.total
      }
      this.pageLoading = false
    },
    async getUsers() {
      const { data } = await UserApi.getUserList({ pageNo: 1, pageSize: 99 })
      if (data && data.total) {
        this.usersOptions = data.items
      }
    },
    async getProjects() {
      const { data } = await ProjectApi.getProjects({ pageNo: 1, pageSize: 99 })
      if (data && data.total) {
        this.projectOptions = data.items
      }
    },
    handleFilter() {
      this.searchQuery.pageNo = 1
      this.getManageDatas()
    },
    handleSelectMonth(val) {
      if (val) {
        this.weekText = week ? weekText : ''
        const { startTime, endTime } = week ? dealWithWeekDay(val) : dealWithMonthDay(val)
        this.searchQuery['start_time'] = startTime
        this.searchQuery['end_time'] = endTime
        this.searchQuery.pageNo = 1
        this.getManageDatas()
      }
    },
    handleChangeRadio() {
      this.searchQuery.pageNo = 1
      this.$forceUpdate()
      this.getManageDatas()
    },
    handleChangeProject() {
      this.searchQuery.pageNo = 1
      this.$forceUpdate()
      this.getManageDatas()
    },
    handleChangeType() {
      this.searchQuery.pageNo = 1
      this.$forceUpdate()
      this.getManageDatas()
    },
    handleChangeUser() {
      this.searchQuery.pageNo = 1
      this.$forceUpdate()
      this.getManageDatas()
    }
  }
}
</script>

<style scoped>
.filter-container {
  display: flex;
  justify-content: space-between;
}
.pagination {
  display: flex;
  justify-content: center;
  margin-top: 1rem;
}
.search-container {
  display: flex;
  align-items: flex-start;
}
</style>
